from PIL import Image
from math import trunc
import os
import xml.etree.ElementTree as ET
import shutil
import cv2

def light(src,dst,light):
    # open an image file (.jpg or.png) you have in the working folder
    im1 = Image.open(src)
    # multiply each pixel by 0.9 (makes the image darker)
    # works best with .jpg and .png files, darker < 1.0 < lighter
    # (.bmp and .gif files give goofy results)
    # note that lambda is akin to a one-line function
    # im1 = cv2.imread(src)
    im2 = im1.point(lambda p: p * light)
    # im1 = img
    # size = img.shape
    # print(size)
    # h,w = size[0],size[1]
    # im2 = im1.point(lambda p: p * light)
    # brings up the modified image in a viewer, simply saves the image as
    # a bitmap to a temporary file and calls viewer associated with .bmp
    # make certain you have associated an image viewer with this file type
    # im2.show()
    # save modified image to working folder as Audi2.jpg
    im2.save(dst)

VOCdevkit_path = 'crane3'
if __name__ == "__main__":
    xmlfilepath = os.path.join(VOCdevkit_path, 'Annotations')
    jpgfilepath = os.path.join(VOCdevkit_path, 'JPEGImages')
    xmlfiles = os.listdir(xmlfilepath)
    xmlFileOutPath = os.path.join(VOCdevkit_path, 'Annotations_out')
    jpgFileOutPath = os.path.join(VOCdevkit_path, 'JPEGImages_out')
    if not os.path.exists(xmlFileOutPath):
        os.mkdir(xmlFileOutPath)
    if not os.path.exists(jpgFileOutPath):
        os.mkdir(jpgFileOutPath)    
    xmlfiles.sort()
    prefixs = ['e_','f_']
    lights = [0.5,1.5]    
    for fileName in xmlfiles:
        jpgFileName = os.path.splitext(fileName)[0]+".jpg"
        for p in prefixs:
            srcXmlfile = os.path.join(xmlfilepath,fileName)
            dstXmlFile = os.path.join(xmlFileOutPath,p+fileName)
            srcJpgFile = os.path.join(jpgfilepath,jpgFileName)
            dstJpgFile = os.path.join(jpgFileOutPath,p+jpgFileName)
            shutil.copyfile(srcXmlfile,dstXmlFile)
            light(srcJpgFile,dstJpgFile,lights[prefixs.index(p)])
            print(dstXmlFile)
            print(dstJpgFile)




